package com.kl.forum.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ListUtil {









    /**
     * 从List中随机出count个对象
     * @param list
     * @param count
     * @return
     */
    public static List randomTopic(List list, int count) {
        // 创建一个长度为count(count<=list)的数组,用于存随机数
        int[] a = new int[count];
        // 利于此数组产生随机数
        int[] b = new int[list.size()];
        int size = list.size();
        // 取样填充至数组a满
        for (int i = 0; i < count; i++) {
            int num = (int) (Math.random() * size); // [0,size)
            int where = -1;
            for (int j = 0; j < b.length; j++) {
                if (b[j] != -1) {
                    where++;
                    if (where == num) {
                        b[j] = -1;
                        a[i] = j;
                    }
                }
            }
            size = size - 1;
        }
        // a填满后 将数据加载到rslist
        List<Object> rslist = new ArrayList<Object>();
        for (int i = 0; i < count; i++) {
            Object df = (Object) list.get(a[i]);
            rslist.add(df);
        }
        return rslist;
    }

    public static void main(String[] args) {
        List list =new ArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        List list1= randomTopic(list,2);
        Iterator iterator = list1.iterator();
//        while(iterator.hasNext()){
//            iterator.next();
//            //do something
//            System.out.println( iterator.next());
//        }
        for (int i=0;i<list1.size();i++){
            System.out.println( list1.get(i));
        }

    }

}